// @const
const { ccclass, } = cc._decorator;

/**
 * 登录场景
 */
@ccclass
export default class SelectHeroScene extends cc.Component {
    /**
     * 选择英雄索引
     */
    _nSelectedHeroIndex: number = -1;

    /**
     * @override
     */
     start() {
        // 给每个英雄添加选择事件
        for (let nI = 0; nI < 3; nI++) {
            let oHeroXXNode: cc.Node = cc.find(`/Canvas/Holder_Hero_${nI}`);

            oHeroXXNode.children.forEach((oChildNode) => {
                oChildNode.color = cc.Color.GRAY;
            });

            oHeroXXNode.on(cc.Node.EventType.TOUCH_END, () => {
                __selectAHero(this, nI);
            });
        }

        cc.find("/Canvas/Button_Entry").on(cc.Node.EventType.TOUCH_END, () => {
            if (this._nSelectedHeroIndex < 0) {
                return;
            }

            cc.director.loadScene("GameScene");
        });
    }
}

////////////////////////////////

/**
 * 选择一个英雄
 * 
 * @param SELF this 指针
 * @param nHeroIndex 英雄索引
 */
 function __selectAHero(SELF: SelectHeroScene, nHeroIndex: number): void {
    if (!SELF || 
        nHeroIndex < 0 || 
        nHeroIndex >= 3) {
        return;
    }

    if (nHeroIndex == SELF._nSelectedHeroIndex) {
        return;
    }
    
    SELF._nSelectedHeroIndex = nHeroIndex;

    for (let nI = 0; nI < 3; nI++) {
        // 获取影响节点
        let oHeroXXNode: cc.Node = cc.find(`/Canvas/Holder_Hero_${nI}`);
        let oSpSkeleton: sp.Skeleton = oHeroXXNode.getComponentInChildren(sp.Skeleton);

        if (nI == nHeroIndex) {
            oHeroXXNode.children.forEach((oChildNode) => {
                oChildNode.color = cc.Color.WHITE;
            });

            oSpSkeleton.setAnimation(0, "Run", true);
            
            cc.tween(oHeroXXNode)
                .to(0.2, { scale: 1.44, y: -100, })
                .start();
        }
        else {
            oHeroXXNode.children.forEach((oChildNode) => {
                oChildNode.color = cc.Color.GRAY;
            });

            oSpSkeleton.setAnimation(0, "Stand", true);
            
            cc.tween(oHeroXXNode)
                .to(0.2, { scale: 1.0, y: -64, })
                .start();
        }
    }
}
